import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import setStorage from '../utils/setstorage'
import store from "@/store";
Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   name: 'layout',
  //   component:import(/* webpackChunkName: "about" */ '../views/layout.vue') ,
  // },
  {
    path: '/',
    name: 'HomeView',
    component: HomeView,
  },
  {
    path: '/learnPage',
    name: 'learnPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/LearnPage/LearnPage.vue')
  },
  {
    path: '/showGua',
    name: 'showGua',
    component: () => import('@/views/showPage/GuaShow.vue')
  },
  {
    path: '/evolution',
    name: 'Evolution',
    component: () => import('@/views/showPage/Evolution.vue')
  },
  {
    path: '/money',
    name: 'Money',
    component: () => import('@/views/showPage/Money.vue')
  },
  {
    path: '/plumBlossom',
    name: 'plumBlossom',
    component: () => import('@/views/showPage/plumBlossom.vue')
  },
  {
    path: '/forum',
    name: 'forum', // 论坛板块
    component: () => import(/* webpackChunkName: "about" */ '../views/ForumView.vue')
  },
  {
    path: '/login',
    name: 'Login', // 登录
    component: () => import(/* webpackChunkName: "about" */ '../views/user/Login.vue')
  },
  {
    path: '/register',
    name: 'Register', // 注册
    component: () => import(/* webpackChunkName: "about" */ '../views/user/Register.vue')
  },
  {
    path: '/showPage',
    component: ()=> import("../views/showPage/showPage.vue")
  },
  {
    path: '/taiji',
    component: () => import("../components/taijiCartition.vue")
  }
]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })


// 解决  router报错 Error: Redirected when going from “/login” to “/posture” via a navigation guard. 的报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)

  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})

/**
 * 编写前端的路由守卫
 */
router.beforeEach((to, from, next) => {
  //
  setStorage.getItem('token')  // 监听token（不加这个的话在token未过期前关闭浏览器，然后在token过期后打开就。。你懂得）
  // 获取用户登录成功后储存的登录标志
  const getFlag = localStorage.getItem('Flag')

  if (getFlag === 'isLogin') { // 已经登陆（这个状态要在你的login请求成功后添加）
    console.log("===守卫:" +setStorage.getItem('token') )

    if (setStorage.getItem('token') === false&&to.path==='/login') {
      localStorage.clear()
      next();
    }

    next();
    //
    // // http://47.115.32.210/
    // if (setStorage.getItem('token') === false&&to.path!=='/login') {
    //   localStorage.clear()
    //   alert( '登录已过期,请重新登录')
    // } else {
    //   // 获取 token 刷新时间
    //   let token = localStorage.getItem('token')
    //   let tokens
    //   if(token!=null){
    //     tokens =  JSON.parse(token)['value']
    //   }
    //
    //   console.log('获取 token 刷新时间')
    //   setStorage.setItem({ // 刷新存储token  刷新过期时间是毫秒
    //     value: tokens,
    //     name: 'token',
    //     // expires: 60000  // 一分钟
    //     expires:  24*2*1800000  // 24*2*1800000  // 24*2*1800000 // 24* 2*30分钟  一天
    //   })
    //   next()
    // }
  }

   if(from.path==='/login') {
     console.log(from.path)
    next();
     return;
  }

   if(from.path==='/register') {
    next();
    return;
  }
  if (!setStorage.getItem('token') && to.path !== '/login' ) {
    // 如果没有 token，则重定向到登录页面
    next({ path: '/login' });
  } else {
    // 否则允许跳转
    next();
  }


})

export default router
